package com.baizhi.controller;


import com.baizhi.entity.Record;
import com.baizhi.entity.User;
import com.baizhi.service.IRecordService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author haiyu
 * @since 2021-08-26
 */
@RestController
@RequestMapping("/record")
public class RecordController {

    @Autowired
    private IRecordService ir;

    @GetMapping("selects/{stuId}")
    public List<Record> selects(@PathVariable("stuId") Integer stuId) {
        QueryWrapper<Record> wrapper = new QueryWrapper<>();
        wrapper.eq("stuId", stuId);
        return ir.list(wrapper);
    }

    @GetMapping("/selectFy")
    public PageInfo<Record> selectFy(Integer pageNo,
                                     Integer pageSize,
                                     String name,
                                     String stuName){

        PageInfo<Record> fy = ir.selectFy(pageNo, pageSize, name, stuName);
    return  fy;
    }
    @PostMapping("/add")//改方法用于添加奖惩
    public void add(@RequestBody Record record, HttpSession
                    session){
        User user =(User) session.getAttribute("user");
        record.setCreateBy(user.getUsername());//获取用户名
        LocalDateTime now = LocalDateTime.now();//Jdk 8之后，可以获取系统时间;
        record.setCreateDate(now);//前端没有对应的列名，控制器手动添加时间
        //save用于添加
        ir.save(record);
    }
    @PutMapping
    public void update(@RequestBody Record record, HttpSession session){
        System.out.println("record = " + record);
        User user =(User) session.getAttribute("user");
        record.setCreateBy(user.getUsername());//获取用户名
        LocalDateTime now = LocalDateTime.now();//Jdk 8之后，可以获取系统时间;
        record.setCreateDate(now);
        ir.updateById(record);//用于修改 要回显

    }
    @GetMapping("/selectOne/{id}")
    public Record selectOne(@PathVariable("id") Integer id){
        Record byId = ir.getById(id);//修改需要回显对象的信息
        return  byId;
    }
    @DeleteMapping("delete/{id}")
    public void delete(@PathVariable("id") Integer id){
        System.out.println("id = " + id);
        ir.removeById(id);
    }
}



